import Vue from 'vue';
import VueRouter from 'vue-router';//使用vue-router必须先引入vue,因为他是vue的一个插件
import { Notification } from 'element-ui'
//引入之后然后使用
Vue.use(VueRouter);

//配置路由
export const constantRoutes = [
    {
        path:'/home',
        name:'home',
        component: () => import('@/pages/Home/index'),
        meta:{isShowFooter:true}
    },
    {
        path:'/login',
        component: () => import('@/pages/Login/index'),
        meta:{isShowFooter:false}
    },
    {
        path:'/register',
        component: () => import('@/pages/Register/index'),
        meta:{isShowFooter:false}
    },
    {
        path:'/search',
        component: () => import('@/pages/Search/index'),
        meta:{isShowFooter:true},
        name:'search',
    },
    {
        path:'/addcartSuccess',
        component: () => import('@/pages/AddCartSuccess/index'),
        meta:{isShowFooter:true},
        name:'addcartSuccess',
    },
    {
        path:'/shopcart',
        component: () => import('@/pages/ShopCart/index'),
        meta:{isShowFooter:true},
        name:'shopcart',
    },
    {
        path:'/detail/:skuid',
        component: () => import('@/pages/Detail/index'),
        meta:{isShowFooter:true},
        name:'detail',
    },
    {
        path:'/trade',
        component: () => import('@/pages/Trade/index'),
        meta:{isShowFooter:true},
        name:'trade',
        //路由独享守卫
        beforeEnter:(to,from,next)=>{
            if(from.path=='/shopcart'){
                next()
            }else{
                Notification({
                    title: '警告',
                    message:'没有订单,请先结算购物车中的商品',
                    duration: 2000,
                    type:'warning'
                })
                next(false)
            }
        }
    },
    {
        path:'/pay',
        component: () => import('@/pages/Pay/index'),
        meta:{isShowFooter:true},
        name:'pay',
        beforeEnter:(to,from,next)=>{
            if(from.path=='/trade'){//如果是从订单页面过来的
                next()
            }else{
                Notification({
                    title: '警告',
                    message:'请先提交订单',
                    duration: 2000,
                    type:'warning'
                })
                next('/home')
            }
        }
    },
    {
        path:'/paysuccess',
        component: () => import('@/pages/PaySuccess/index'),
        meta:{isShowFooter:true},
        name:'paysuccess',
    },
    {
        path:'/center',
        component: () => import('@/pages/Center/index'),
        meta:{isShowFooter:true},
        name:'center',
        children:[
            {
                path:"myorder",
                component: () => import('@/pages/Center/myOrder/index'),
            },
            {
                path:"grouporder",
                component: () => import('@/pages/Center/groupOrder/index'),
            }

        ]
    },
    {
        path: '/communication',
        component: () => import('@/pages/Communication/Communication'),
        children: [
          {
            path: 'event',
            component: () => import('@/pages/Communication/EventTest/EventTest'),
            meta: {
              isHideFooter: true
            },
          },
          {
            path: 'model',
            component: () => import('@/pages/Communication/ModelTest/ModelTest'),
            meta: {
              isHideFooter: true
            },
          },
          {
            path: 'sync',
            component: () => import('@/pages/Communication/SyncTest/SyncTest'),
            meta: {
              isHideFooter: true
            },
          },
          {
            path: 'attrs-listeners',
            component: () => import('@/pages/Communication/AttrsListenersTest/AttrsListenersTest'),
            meta: {
              isHideFooter: true
            },
          },
          {
            path: 'children-parent',
            component: () => import('@/pages/Communication/ChildrenParentTest/ChildrenParentTest'),
            meta: {
              isHideFooter: true
            },
          },
          {
            path: 'scope-slot',
            component: () => import('@/pages/Communication/ScopeSlotTest/ScopeSlotTest'),
            meta: {
              isHideFooter: true
            },
          }
        ],
      },
    {//重定向
        path:'*',
        redirect:'/home'
    }
]

const router = new VueRouter({
    // mode: 'history',
    routes: constantRoutes,
    scrollBehavior (to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition
        } else {
            return { x: 0, y: 0 }
        }
    }
})

export default router;